package com.cloud.wms.ui.returned.inquire;

import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.returned.contact.InquireContact;
import com.cloud.wms.ui.returned.entity.InvoiceEntity;
import com.cloud.wms.ui.returned.presenter.InquirePresenter;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FC on 2021/5/20.
 * Desc：退货面单查询
 */
public class FaceSheetStartActivity extends BaseBarListActivity<TransferCommodityEntity, InquirePresenter> implements InquireContact.View {
    @BindView(R.id.scan_order)
    ScanText scanOrder;
    @BindView(R.id.scan_bar_code)
    ScanText scanBarCode;
    @BindView(R.id.et_phone)
    EditText etPhone;

    @Override
    protected String barTitle() {
        return "退货面单查询";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_face_sheet_start;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanOrder);
        initScanText(scanBarCode);
        scanOrder.getFocus();
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_face_sheet_start;
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, TransferCommodityEntity item) {
        helper.setText(R.id.tv_bar_code, item.getBarCode())
                .setText(R.id.tv_commodity_name, item.getCommodityName());
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanBarCode.addHistory(result);
        if (scanBarCode.isGetFocus()) {
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                if (result.equalsIgnoreCase(mAdapter.getItem(i).getBarCode())) {
                    info("该商品已添加");
                    return;
                }
            }
            mPresenter.checkCommodity(result);
        }
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case InquirePresenter.CHECK_COMMODITY:
                TransferCommodityEntity commodityEntity = (TransferCommodityEntity) message.obj;
                mAdapter.addData(0, commodityEntity);
                rvList.smoothScrollToPosition(0);
                break;
            case InquirePresenter.RETURN_ORDER_INQUIRE:
                ArrayList<InvoiceEntity> invoiceEntityList = (ArrayList<InvoiceEntity>) message.obj;
                ReturnOrderInquireActivity.jumpToNext(this, scanOrder.getText().trim(), invoiceEntityList);
                scanOrder.setText("");
                etPhone.setText("");
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
                break;
        }
    }

    @OnClick(R.id.btn_confirm)
    public void onClick() {
        if (TextUtils.isEmpty(scanOrder.getText().trim())) {
            info("面单号不能为空");
            return;
        }
        if (TextUtils.isEmpty(etPhone.getText().toString().trim())) {
            info("电话号码不能为空");
            return;
        }
        if (mAdapter.getData().size() == 0) {
            info("请至少录入一件商品");
            return;
        }
        List<String> commodityIDList = new ArrayList<>();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            commodityIDList.add(mAdapter.getItem(i).getCommodityID());
        }
        mPresenter.confirmSelect(scanOrder.getText().trim(), etPhone.getText().toString().trim(), commodityIDList);
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<TransferCommodityEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        new MaterialAlertDialogBuilder(this)
                .setTitle("删除确认")
                .setMessage("确认删除该商品吗？")
                .setPositiveButton("确认", (dialog, which) -> {
                    mAdapter.removeAt(position);
                })
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
